// pages/coupon/index.js
import {
    marketing,
    user,
    wxapp
} from '../../api/apiData'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        type: '',
        list: [],
        use_scene: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {},
    /**
     * 授权登录
     */
    getUserInfos(e) {
        let that = this
        wx.login({
            success(res) {
                console.log(res)
                let code = res.code
                wx.getUserInfo({
                    success: (res) => {
                        var encryptedData = res.encryptedData;
                        var iv = res.iv
                        var info = res.rawData
                        var obj = {
                            code: code,
                            iv: iv,
                            encryptedData: encryptedData
                        }
                        wxapp.login(obj, res => {
                            if (res.code == 1) {
                                wx.setStorage({
                                    key: "token",
                                    data: res.data.token
                                })
                                that.getList()
                            } else {
                                wx.showToast({
                                    title: res.msg,
                                    icon: 'none'
                                })
                            }
                        })
                    },
                    fail: res => {

                    }
                })
            }
        })
    },
    getList() {
        let type = this.data.type
        if (type == 0) {
            type = ''
        }
        marketing.coupon.lists({
            type: type,
            use_scene: parseInt(this.data.use_scene)
        }, res => {
            if (res.code == 1) {
                this.setData({
                    list: res.data
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    toDetail(e) {
        console.log(e)
        let item = e.currentTarget.dataset.item
        if (item.use_scene == 1) {
            wx.switchTab({
                url: '/pages/index/index',
            })
        }
        if (item.use_scene == 2) {
            if (item.use_merchant_list.length == 1) {
                wx.navigateTo({
                    url: '/pages/hotel/detail?id=' + item.use_merchant_list[0].id,
                })
            } else {
                wx.navigateTo({
                    url: '/pages/hotel/index',
                })
            }

        }
        if (item.use_scene == 3) {
            if (item.use_merchant_list.length == 1) {
                wx.navigateTo({
                    url: '/pages/shopping/detail?id=' + item.use_merchant_list[0].id,
                })
            } else {
                wx.navigateTo({
                    url: '/pages/shopping/index',
                })
            }
        }
        if (item.use_scene == 4) {
            if (item.use_merchant_list.length == 1) {
                wx.navigateTo({
                    url: '/pages/food/detail?id=' + item.use_merchant_list[0].id,
                })
            } else {
                wx.navigateTo({
                    url: '/pages/food/index',
                })
            }
        }
    },
    onChange(e) {
        this.setData({
            type: e.detail.index
        })
        this.getList()
    },
    toActive(e) {
        let type = e.currentTarget.dataset.type
        if (type != '') {
            type = Number(type)
        }
        this.setData({
            use_scene: type
        })
        this.getList()
    },
    toReceive(e) {
        let id = e.currentTarget.dataset.id
        marketing.coupon.receive({
            coupon_id: id
        }, res => {
            if (res.code == 1) {
                wx.showToast({
                    title: res.msg
                })
                setTimeout(() => {
                    this.getList()
                }, 1000)
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getInfo()
        this.getList()
    },
    getInfo() {
        let token = wx.getStorageSync('token')
        if (token) {
            // 获取用户详情
            user.detailS({}, res => {
                if (res.code == 20003) {
                    this.getUserInfos()
                } else {
                    this.setData({
                        info: res.data
                    })
                    this.getList()
                }

            })
        } else {
            this.getUserInfos()

        }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})